package com.sistemagestion.ventas.view;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sistemagestion.R;
import com.sistemagestion.BO.ClienteBO;
import com.sistemagestion.BO.FacturaBO;
import com.sistemagestion.BO.FormaPagoBO;
import com.sistemagestion.Entities.Cliente;
import com.sistemagestion.Entities.Factura;
import com.sistemagestion.Entities.FormaPago;

public class VentaAdapter extends ArrayAdapter<Factura> {

	private int res;
	FacturaBO facturaBO = new FacturaBO();
	FormaPagoBO formaPagobo = new FormaPagoBO();
	private List<Factura> _faturas;
	ClienteBO clienteBO = new ClienteBO();

	public VentaAdapter(Context context, int textViewResourceId,
			List<Factura> objects) {
		super(context, textViewResourceId, objects);
		this.res = textViewResourceId;
		this._faturas = objects;
	}

	@Override
	public void add(Factura object) {
		super.add(object);
	}

	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public Factura getItem(int position) {
		return _faturas.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null){
			viewholder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(res, null);
			viewholder.txtCliente = (TextView)convertView.findViewById(R.id.txtCliente);
			viewholder.txtNumeroFactura = (TextView)convertView.findViewById(R.id.txtNumeroFactura);
			viewholder.txtTotal = (TextView)convertView.findViewById(R.id.txtTotal);
			viewholder.txtFormaPago = (TextView)convertView.findViewById(R.id.txtFormaPago);
			viewholder.color = (View)convertView.findViewById(R.id.color);
			convertView.setTag(viewholder);
		}
		else {
			viewholder = (ViewHolder)convertView.getTag();
		}
		
		Factura factura = (Factura)getItem(position);
		FormaPago formaPago = formaPagobo.getById(factura.getFormaPago());
		Cliente cliente = clienteBO.getById(factura.getIdCliente());
		viewholder.txtCliente.setText(cliente != null ? (cliente.getNombreApellido()): "");
		viewholder.txtNumeroFactura.setText(Integer.toString(factura.getNumeroFactura()));
		viewholder.txtTotal.setText((factura.getTotal()).toString());
		viewholder.txtFormaPago.setText(formaPago.getFormaPago());
		viewholder.color.setBackgroundResource(R.drawable.gradientgreen);
		return convertView;
	}

	private static class ViewHolder{
		TextView txtNumeroFactura;
		TextView txtCliente;
		TextView txtTotal;
		TextView txtFormaPago;
		View color;
	}
	@Override
	public void remove(Factura object) {
		super.remove(object);
	}
}
